package net.aiion.weave.impl.cards.expressions;

import net.aiion.weave.spi.cards.CardException;
import net.aiion.weave.spi.cards.expressions.CardExpression;
import net.aiion.weave.spi.cards.expressions.CardExpressionResolver;
import net.aiion.weave.spi.naming.annotations.Named;
import net.aiion.weave.spi.naming.annotations.UserScoped;

@UserScoped
@Named
public class CardLiteralExpressionResolver implements CardExpressionResolver
{
    private static final String LITERAL_PREFIX = "literal";

    public CardLiteralExpressionResolver() {
    }

    @Override
    public boolean resolvesExpression(final CardExpression aCardExpression) {
        return LITERAL_PREFIX.equals(aCardExpression.getPrefix());
    }

    @Override
    public Object readExpression(final CardExpression aCardExpression) {
        return aCardExpression.getValue();
    }

    @Override
    public void writeExpression(final CardExpression aCardExpression, final Object aValue) {
        throw new CardException(String.format("Cannot write a literal value (Expression: '%s')",
            aCardExpression.toString()));
    }
}
